package com.scorpio.appuser.controller;

import com.scorpio.appuser.domain.UserEntity;
import com.scorpio.appuser.dto.FressTotaiDTO;
import com.scorpio.appuser.service.ChatMessageServer;
import com.scorpio.appuser.vo.LocationInfoVo;
import com.scorpio.appuser.vo.SendChatMassageVo;
import com.scorpio.common.annotation.Log;
import com.scorpio.common.core.controller.BaseController;
import com.scorpio.common.core.domain.Result;
import com.scorpio.scwallet.service.WalletRecordService;
import com.scorpio.security.annotation.PersonalLogin;
import com.scorpio.security.annotation.PersonalLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/chat")
public class ChatMessageController extends BaseController {


    @Autowired
    private ChatMessageServer messageServer;
    @Autowired
    private WalletRecordService walletRecordService;


    @Log(title = "发送消息前的操作")
    @PersonalLogin
    @PostMapping("appendSendMessage")
    public Result<FressTotaiDTO> appendSendMessage(@PersonalLoginUser UserEntity entity, SendChatMassageVo vo) {
        FressTotaiDTO dto = messageServer.appendSendMessage(entity, vo.getToUserId());
        if (vo.getToUserId() != null) {
            int count = walletRecordService.countPayChatTotali(entity.getId(), vo.getToUserId());
            dto.setMaxTotal(count > 0 ? Integer.MAX_VALUE : dto.getMaxTotal());
        }
        return Result.success(dto);
    }

    @Log(title = "全局初始化")
    @PersonalLogin
    @PostMapping("globalInit")
    public Result globalInit(@PersonalLoginUser UserEntity entity, @Validated LocationInfoVo vo) {
        messageServer.massSendMessage(entity, vo);
        return Result.success();
    }
}
